package mytest;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.PipedOutputStream;
import java.io.Serializable;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.omg.CORBA_2_3.portable.OutputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ObjectCreator implements Runnable {

	private InputStream inputStream = null;
	private PipedOutputStream outputStream = null;

	public ObjectCreator(InputStream inputStream, PipedOutputStream objSender) {
		this.inputStream = inputStream;
		this.outputStream = objSender;
	}

	public static Document readXml(InputStream is) throws SAXException,
			IOException, ParserConfigurationException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		dbf.setValidating(false);
		dbf.setIgnoringComments(false);
		dbf.setIgnoringElementContentWhitespace(true);
		dbf.setNamespaceAware(true);
		 dbf.setCoalescing(true);
		 dbf.setExpandEntityReferences(true);

		DocumentBuilder db = null;
		db = dbf.newDocumentBuilder();
		db.setEntityResolver(new NullResolver());

		// db.setErrorHandler( new MyErrorHandler());

		return db.parse(is);
	}
	public ArrayList<Person> createObjects (Document doc) {
		ArrayList<Person> temp = new ArrayList<>();
		NodeList nodeListPersons = doc.getChildNodes();
		nodeListPersons.getLength();
		for (int i = 0, lenI = nodeListPersons.getLength(); i < lenI; i++) {
			 Node tempNodePerson = nodeListPersons.item(i);
			 NodeList singlePerson = tempNodePerson.getChildNodes();
			 for(int j = 1, lenJ = singlePerson.getLength(); j < lenJ; j+=2){
				 Node personObj = singlePerson.item(j);
				 NodeList atributes = personObj.getChildNodes();
				 int age = Integer.parseInt(atributes.item(5).getTextContent());
				 temp.add(new Person(atributes.item(1).getTextContent(), atributes.item(3).getTextContent(), age));
			 }
		}
		return temp;
	}
	@Override
	public void run() {
		try {
			Document doc = readXml(this.inputStream);
			ArrayList<Person> persons = createObjects(doc);
			
			ObjectOutputStream oo=new ObjectOutputStream(outputStream);
			oo.writeObject(persons);

		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class NullResolver implements EntityResolver {
	public InputSource resolveEntity(String publicId, String systemId)
			throws SAXException, IOException {
		return new InputSource(new StringReader(""));
	}
}

class Person implements Serializable {
	private static final long serialVersionUID = 7526472295622776147L;
	private String nume;
	private String prenume;
	private int age;

	public Person(String nume, String prenume, int age) {

		this.nume = nume;
		this.prenume = prenume;
		this.age = age +10;

	}
	
	public String getNume () {
		return this.nume;
	}
	public String getPrenume () {
		return this.prenume;
	}
	public int getAge () {
		return this.age;
	}
	
	public void setNume (String nume) {
		this.nume = nume;
	}
	public void setPrenume (String prenume) {
		this.prenume = prenume;
	}
	public void setAge (int age) {
		this.age = age;
	}

}
